<?php


namespace App\Services\Manager;


use App\Models\Cash;
use App\Models\Shop;
use App\Models\UserManager;

class ShopService
{
    public function shopList($data)
    {
        $sql = Shop::select(
            'shopId',
            'shopName',
            'branchNum',
            'province',
            'city',
            'area',
            'street',
            'address',
            'responsibleName',
            'phone',
            'QQ',
            'email',
            'registerTime',
            'auditStatus',
            'forbiddenStatus',
            'expireTime',
            'noteTotalNum',
            \DB::raw('noteTotalNum - noteUseNum as noteSurplusNum')
        );
        if(!isset($data['isTest'])){
            $sql->where('isTest',0);
        }
        if(isset($data['shopId'])){
            $shopId = $data['shopId'];
            $sql->where('shopId',$shopId);
        }
        if(isset($data['address'])){
            $address = $data['address'];
            $sql->where(function ($query) use ($address){
                $query->where('province','like','%'.$address.'%')
                    ->orWhere('city','like','%'.$address.'%')
                    ->orWhere('area','like','%'.$address.'%')
                    ->orWhere('street','like','%'.$address.'%')
                    ->orWhere('address','like','%'.$address.'%');
            });
        }
        if(isset($data['mainShopId'])){
            $mainShopId = $data['mainShopId'];
            $sql->where('mainShopId',$mainShopId);
        }else{
            $sql->where('mainShopId',null);
        }

        if(isset($data['keyword'])){
            $keyword = $data['keyword'];
            $sql->where(function($query) use ($keyword){
                $query->where('responsibleName','like','%'.$keyword.'%')->orWhere(function($query) use ($keyword){
                    $query->where('phone','like','%'.$keyword.'%');
                });
            });
        }
        if(isset($data['shopName'])){
            $shopName = $data['shopName'];
            $sql->where('shopName','like','%'.$shopName.'%');
        }
        if(isset($data['QQ'])){
            $QQ = $data['QQ'];
            $sql->where('QQ','like','%'.$QQ.'%');
        }
        if(isset($data['province'])){
            $province = $data['province'];
            $sql->where('province','like','%'.$province.'%');
        }
        if(isset($data['city'])){
            $city = $data['city'];
            $sql->where('city','like','%'.$city.'%');
        }
        if(isset($data['area'])){
            $area = $data['area'];
            $sql->where('area','like','%'.$area.'%');
        }
        if(isset($data['street'])){
            $street = $data['street'];
            $sql->where('street','like','%'.$street.'%');
        }
        if(isset($data['auditStatus'])){
            $auditStatus = $data['auditStatus'];
            $sql->where('auditStatus',$auditStatus);
        }
        if(isset($data['startDate'])){
            $startDate = $data['startDate'];
            $sql->where('expireTime','>=',$startDate);
        }
        if(isset($data['endDate'])){
            $endDate = $data['endDate'];
            $sql->where('expireTime','<=',$endDate);
        }
        if(isset($data['forbiddenStatus'])){
            $forbiddenStatus = $data['forbiddenStatus'];
            $sql->where('forbiddenStatus',$forbiddenStatus);
        }

        if(isset($data['orderBy'])){
            $orderBy = $data['orderBy'];
            $sql->orderBy($orderBy['orderBy'],$orderBy['rule']);
        }else{
            $sql->orderBy('createTime','desc');
        }

        if(isset($data['pageSize'])){
            $pageSize = $data['pageSize'];
            $shopList = $sql->paginate($pageSize)->toArray();
            unset($data);
            $data['shopList'] = $shopList['data'];
            $data['totalNum'] = $shopList['total'];
        }else if(isset($data['first'])){
            $shopList = $sql->first();
            $data = $shopList;
        }else{
            if(isset($data['take'])){
                $take = $data['take'];
                $sql->take($take);
            }
            $shopList = $sql->get();
            unset($data);
            $data = $shopList;
        }
        return $data;
    }

    public function addCash($data,$shopId,$managerId)
    {
        if(!$data['cashType'] || !$data['changeCash']){
            return 0;
        }
        $managerNmae = UserManager::where('managerId', $managerId)->value('name');
        $data['createTime'] = date("Y-m-d H:i:s");
        $data['shopId'] = $shopId;
        $data['managerId'] = $managerId;
        $data['userName'] = $managerNmae;
        $shopCash = Shop::where('shopId',$shopId)->value('cash');
        if($data['cashType'] == "OUT"){
            $data['surplusCash'] = $shopCash-$data['changeCash'];
            Shop::where('shopId',$shopId)->increment('cash',-$data['changeCash']);
        }else{
            $data['surplusCash'] = $shopCash+$data['changeCash'];
            Shop::where('shopId',$shopId)->increment('cash',$data['changeCash']);
        }

        Cash::insert($data);
        return 1;
    }
}